package com.survey.Controller;

import com.survey.utils.TwoDimensionCode;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;
//TODO
/**
 * 如果没有部署到服务器上，只能用当前电脑的IP地址，端口号是当前项目Tomcat端口号
 * 例如：当前电脑IP：192.168.1.109  端口号：3210
 * 想用客户端测试，改为自己的IP，端口即可。并用电脑开热点，让手机连上该热点
 * 二维码没10秒更新
 */
@Controller
public class ScanQR {
    @RequestMapping(value = "/getTwoDemensionCode")
    @ResponseBody
    public String getTwoDemensionCode(HttpServletRequest request) {
        String uuid = UUID.randomUUID().toString().substring(0, 8);
        String ip = "192.168.1.119";//这里是服务器的ip(现在只能用本机的ip地址)
        //二维码内容
        String content = "http://" + ip + ":3210/index?uuid=" + uuid;
        System.out.println("二维码的 uuid:" + uuid);
        //生成二维码
        String imgName = uuid + "_" + (int) (new Date().getTime() / 1000) + ".png";
        String imgPath = request.getServletContext().getRealPath("/") + "images" + "/" + imgName;
        TwoDimensionCode handler = new TwoDimensionCode();
        handler.encoderQRCode(content, imgPath, "png");

        //生成的图片访问地址
        String qrCodeImg = "http://" + "127.0.0.1" + ":3210/" + "images" + "/" + imgName;

        JSONObject json = new JSONObject();
        json.put("uuid", uuid);
        json.put("qrCodeImg", qrCodeImg);
        return json.toString();
    }
}
